$(function(){

$.fx.speeds._default = 1000;

    getAllParmtoQueryByPage(1);
    
    //跳转时
    $("#jumpPage").click(function(){
        getAllParmtoQueryByPage($("#jump").attr("value"));
    });
    /**
     * 多重查询
     */
    $("#query").click(function(){
        getAllParmtoQueryByPage(1);
    });
    /**
     * 重置
     */
    $("#q_rest").click(function(){
        reset();
    });
    
    /**
     * 上一页
     */
    $("#nextPage").click(function(){
        getAllParmtoQueryByPage(parseInt($("#page").text()) + 1);
    });
    
    /**
     * 下一页
     */
    $("#prePage").click(function(){
        getAllParmtoQueryByPage(parseInt($("#page").text()) - 1);
    });
    
    /**
     * 首页
     * @param {Object} 1
     */
    $("#firstPage").click(function(){
        getAllParmtoQueryByPage(1);
    });
    
});


function reset(){
    $("#roleId").text("");
    $("#dept").attr("value", "");
    $("#dept").text("");
    $("#q_UName").attr("value", "");
    
}


/**
 *
 * @param {Object} page 第几页,其它的参数会自动从页面获取,若为空,则不把限制加进去
 */
function getAllParmtoQueryByPage(page){
    var roleId = $("#roleId").text();
    var deptId = $("#dept").attr("value");
    var uName = $("#q_UName").attr("value");
    //    alert($("#roleId").text());
    //    alert($("#dept").attr("value"));
    //    alert($("#q_UName").attr("value"));
    
    req_queryTbUser = {
        'pageHelp.currentPage': page,
        'tbUser.name': uName,//
        'roleId': roleId,
        'deptId': deptId
    };
    showListByParmAndRequest("oa-user/tbUserAction_queryTbUser.action", req_queryTbUser);
    
}

/**
 * 传参数和请求,然后显示在页面上
 * @param {Object} url_queryTbUser
 * @param {Object} req_queryTbUser
 */
function showListByParmAndRequest(url_queryTbUser, req_queryTbUser){

    $("tbody#listUser").empty();//清空
    $.post(url_queryTbUser, req_queryTbUser, function(data){
        //填充完表格
        $.each(data.pageUtilUser.list, function(i, o){
            var hs = " <tr>";
            hs += "<td id='userId' >" + o.tbUserId + "</td>";
            hs += "<td>" + o.name + "</td>";
            
            //填角色
            hs += "<td>"
            $.each(o.tbUserRoleEntities, function(i1, o1){
                hs += o1.tbRole.name + "\n";
            });
            hs += "</td>";
            
            //填部门
            hs += "<td>"
            $.each(o.tbUserDepartment, function(i1, o1){
                hs += o1.tbDepartment.name + "\n";
            });
            hs += "</td>";
            
            //时间,未完成 
            hs += "<td>" + o.lastLoginTime + "</td>";
            
            hs += "<td><span class = 'link editUser'>编辑</span></td>";
            hs += "<td><span class = 'link deleteUser'>删除</span></td>";
            hs += "</tr>";
            $("tbody#listUser").append(hs);
        });
        //总数
        $("#total").text(data.pageUtilUser.total);
        //当前页
        $("#page").text(data.pageUtilUser.page);
        //每页条数
        $("#maxRow").text(data.pageUtilUser.maxRow);
        
        //这里要把deleteUser绑定起下面的动作
        $("span.deleteUser").click(function(){
			if(confirm("确定删除记录?")){
				var userId = $(this).parent().parent().children("#userId").text();
				url_deleteUserById="oa-user/tbUserAction_deleteUserById.action";
				req_deleteUserById={
					'tbUser.tbUserId':userId
				};
				$.post(url_deleteUserById,req_deleteUserById,function(data){
					getAllParmtoQueryByPage(parseInt($("#page").text()));
					alert(data.result);
				});
			}
        });
        
        //这里要把editUser绑定起下面的动作
        $("span.editUser").click(function(){
			var userId = $(this).parent().parent().children("#userId").text();
			window.location="/hg/oa-user/tbUserAction_queryUserById.action?tbUser.tbUserId="+userId;
        });
        
    });
    $("tr#loadingUser").css("display", "none");
}
